var ajaxUrl = require('../../../utils/url.js');
var encrypt = require('../../../utils/encrypt.js');
const Validate = {
  mobile: function (num) { // 验证手机
    return /^1[3|4|5|7|8][0-9]\d{8}$/.test(num)
  }
}
Page({
  data: {
    addressId: 0,
    form: {},
    showFlag: false
  },
  onLoad: function (options) {
    const self = this
    if(options.addressId){
      self.setData({
        addressId: options.addressId
      })
    }
    wx.getStorage({
      key: 'addressData',
      success: function(res) {
        if(res.data.addressId == self.data.addressId){
          
          self.setData({
            form: res.data
          })
        }
      } 
    })
  },
  switch1Change: function (e){
    console.log('switch1 发生 change 事件，携带值为', e.detail.value)
  },
  bindName: function(e) {
    var str = 'form.name'
    const self = this
    self.setData({
      [str]: e.detail.value
    })
  },
  bindContact (e) {
    var str = 'form.contactNumber'
    const self = this
    self.setData({
      [str]: e.detail.value
    })
  },
  bindDetailed (e) {
    var str = 'form.detailedAddress'
    const self = this
    self.setData({
      [str]: e.detail.value
    })
  },
  switch1Change (e) {
    var str = 'form.defaultAddressFlag'
    const self = this
    self.setData({
      [str]: e.detail.value
    })
  },
  
  chooseAddress: function () {
    const self = this
    self.setData({
      showFlag: !self.data.showFlag
    })
  },
  close () {
    const self = this
    self.setData({
      showFlag: false
    })
  },
  select (e) {
    let data = e.detail
    const self = this
    var provinceName = 'form.provinceName'
    var cityName ='form.cityName'
    var countyName = 'form.countyName'
    var streetName = 'form.streetName'
    var loungeBoard = 'form.loungeBoard'
    var provinceCode = 'form.provinceCode'
    var cityCode = 'form.cityCode'
    var countyCode = 'form.countyCode'
    var streetCode = 'form.streetCode'
    var loungeBoardCode = 'form.loungeBoardCode'
    self.setData({
      [provinceName]: data.province.name,
      [cityName]: data.city.name,
      [countyName]: data.county.name,
      [streetName]: data.street.name,
      [loungeBoard]: data.village.name,
      [provinceCode]: data.province.code,
      [cityCode]: data.city.code,
      [countyCode]: data.county.code,
      [streetCode]: data.street.code,
      [loungeBoardCode] : data.village.code,
      showFlag : false
    })
  },
  limitNull () {
    const self = this
    if(self.data.form.name && self.data.form.contactNumber && self.data.form.provinceName && self.data.form.detailedAddress){
      return true
    }
  },
  submit () {
    const self = this
    if(!self.limitNull()){
      wx.showModal({
        content: '请填写完整信息',
        showCancel: false,
        confirmText: "确定"
      })
      return
    }
    if (!Validate.mobile(self.data.form.contactNumber)) {
      wx.showModal({
        content: "请填写正确的手机号",
        showCancel: false,
        confirmText: "确定"
      })
      return
    }
    if(self.data.form.addressId){
      let params = {
        addressId: self.data.form.addressId,
        name: self.data.form.name,
        contactNumber: self.data.form.contactNumber,
        provinceCode: self.data.form.provinceCode,
        cityCode: self.data.form.cityCode,
        countyCode: self.data.form.countyCode,
        streetCode: self.data.form.streetCode,
        defaultAddressFlag: self.data.form.defaultAddressFlag,
        detailedAddress: self.data.form.detailedAddress,
      }
      params = encrypt.encrypt(params)
      wx.request({
        method: "POST",
        url: ajaxUrl.updateAddress(),
        data: params,
        success: function(res){
          if(res.data.code === 200){
            wx.navigateBack({
              delta: 1
            })
            // wx.navigateTo({
            //   url: '../addressList/addressList'
            // });
          }else{
            console.error(res.data.msg)
          }
        }
      })
    }else{
      let params2 = {
        name: self.data.form.name,
        contactNumber: self.data.form.contactNumber,
        provinceCode: self.data.form.provinceCode,
        cityCode: self.data.form.cityCode,
        countyCode: self.data.form.countyCode,
        streetCode: self.data.form.streetCode,
        defaultAddressFlag: self.data.form.defaultAddressFlag,
        detailedAddress: self.data.form.detailedAddress,
      }
      params2 = encrypt.encrypt(params2)
      wx.request({
        method: "POST",
        url: ajaxUrl.addAddress(),
        data: params2,
        success: function(res){
          if(res.data.code === 200){
            wx.showToast({
              title: '地址保存成功',
              icon: 'success',
              duration: 1000,
              mask:true
            })
            wx.navigateBack({
              delta: 1
            })
            // wx.navigateTo({
            //   url: '../addressList/addressList'
            // });
          }else{
            console.error(res.data.msg)
          }
        }
      })
    }

  }
})